define(['jquery', 'api', 'user', 'util'], function ($, api, user, util) {
    var vm,
    	wxid,
            controller = {
                init: function (id) {
                    if (!user.checkLogin())
                        return;
                        
                    api.headerShow(1);
					api.headerTitle(L('html_title_pass_getfund'));
                    api.setTitle(L('html_title_pass_getfund'));
                    wxid=api.getWxid()?api.getWxid():'';
                    this.render();
                },
                render: function () {
                    var that = this;
                    $('#common').html('<div id="getFund"></div>');
                    require(['text!pages/pass/getfund.html', 'Vue'], function (tpl, Vue) {
                        vm = new Vue({
                            el: '#getFund',
                            data: {
                                step: 1,
                                show: true,
                                form:{
                                	mobile:user.getMobile(),
                                    verifycode: '',// 手机短信码
                                    captcha: '',// 图片验证码
                                    moneypwd: '',
                                    remoneypwd: ''
                                },
                                imgSrc: false,
                                wxid:wxid,
                                hasMobile: user.getMobile()?true:false
                            },
                            methods: {
                                changeCaptcha:function(){
                                    that.loadCaptcha();
                                },
                                getVerify: function () { // 获取手机短信码
                                    if(this.form.captcha == ''){
                                        util.msg('请先输入动态码！');
                                    }else{
                                        util.Verify.send(this.form.mobile,this.form.captcha,$('#btnVerify'));
                                    }
                                },
                                checkMobile: function () { // 下一步
                                	var that=this;
                                    if (this.form.verifycode == '') {
                                        util.msg('请输入验证码！');
                                        return false;
                                    }
                                    
                                    api.pwdCheck({smcode:this.form.verifycode},function(rs){
                                        if(rs.retcode=='0'){
                                            that.step=2;
                                        }else{
                                            util.dealErrorCode(rs.retcode);// 20161209dyx:增加提示信息
                                        }
                                    });
                                },
                                resetPwd: function () {
                                    var error = '';
                                    if (typeof this.form.moneypwd == 'undefined' || this.form.moneypwd == '') {
                                        error = '请输入新密码';
                                    } else if (typeof this.form.remoneypwd == 'undefined' || this.form.remoneypwd == '') {
                                        error = '请确认新密码';
                                    } else if (this.form.moneypwd !== this.form.remoneypwd) {
                                        error = '两次输入密码不一致！'
                                    }
                                    
                                    if (error) {
                                        util.msg(error);
                                    } else {
                                        api.resetPwd({wxid:vm.wxid,mobile:this.form.mobile,pwd: this.form.moneypwd, pwdtype: 50},function(rs){
                                        	 if(rs.retcode==0){
                                        	    util.msg('重置成功！');
                                                vm.form={
				                                	mobile:user.getMobile(),
				                                    verifycode: '',// 手机短信码
				                                    captcha: '',// 图片验证码
				                                    moneypwd: '',
				                                    remoneypwd: ''
				                                };
				                                
                                        	    setTimeout(function(){
                                        	    	vm.step = 1;
	                                                location.href = "#/";
                                                },300);
                                        	 }else{
                                        	     util.dealErrorCode(rs.retcode);
                                        	 }
                                        });
                                    }
                                },
                                changeCaptcha: function () {
                                    that.loadCaptcha();
                                }
                            },
                            template: tpl
                        });
                        that.loadCaptcha();//20161210dyx
                    })
                },
//              loadWxid:function(){
//              	api.mobileToWxid({
//              	    data:{mobile:vm.form.mobile},
//              	    success:function(rs){
//              	        if(rs.retcode=='0'){
//              	        	if(rs.data.wxid!==''){
//              	        	  vm.wxid=rs.data.wxid;
//              	              vm.step=2;
//              	        	}else if(rs.retcode=='43'){
//                      	 	 vm.step = 1;
//                      	     util.dealErrorCode(rs.retcode);
//                      	    }else{
//              	        	   util.msg('帐号不存在！');
//              	        	}
//              	        }
//              	    }
//              	})
//              },
                loadCaptcha: function () {
                    api.captcha(function (rs) {
                        if (rs.retcode == '0') {
                            vm.imgSrc = 'data:image/png;base64,' + rs.imgcode;
                        }
                    })
                },
                resetForm:function(){ //清除表单
		       		if(vm){
		       			for (i in vm.form) {
	   	         			vm.form[i] = '';
	   	         		}
		       		}
		       },
                onRouteChange: function () {
                    if (vm){
	                    vm.show = false;
	                    vm.step = 1;
	                    this.resetForm();
	                }
                }
            };
    return controller;
})